<?php

# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
    'appId'  => '127661427389840',
    'secret' => 'f864f1d009c17d98b7b14050b1ac2283',
    'cookie' => true
));

Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
	$friends = $facebook->api('/me/friends');
	//var_dump($user_profile);
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  //$loginUrl = $facebook->getLoginUrl();
  $loginUrl = $facebook->getLoginUrl(array(
	'req_perms' => 'likes',
    //'next' => 'http://'.$_SERVER['SERVER_NAME'].'/success.php',
    //'cancel_url' => 'http://'.$_SERVER['SERVER_NAME'].'/cancel.php'
  ));
  header("Location: {$loginUrl} ");  
}

/*if ($user) {
	// enter database info
	$query = " INSERT INTO recomandari ('fb_id', 'fb_name', 'fb_email', 'fb_friend_id', 'fb_friend_name')
	VALUES ('".$user_profile['id']."', '".$user_profile['name']."'
}*/


/*
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>

      <h3>Your Friends (/me/friends)</h3>
      <pre><?php print_r($friends); ?></pre>

      <h3>Traian Rebedea stuff</h3>
      <pre><?php $str = file_get_contents('https://graph.facebook.com/509585803');
	  $userarr = json_decode($str);
	  print_r($userarr); ?>
	  <?php $str = file_get_contents('https://graph.facebook.com/509585803/likes');
	  $$likes = json_decode($str);
	  print_r($userarr); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>
  </body>
</html>*/



